//
//  MLRuntime.m
//  Pods
//
//  Created by molon on 15/6/25.
//
//

#import "MLRuntime.h"
#import <objc/runtime.h>

NSDictionary *GetPropToTypesOfClass(Class cls)
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList(cls, &outCount);
    for(i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        const char *propName = property_getName(property);
        const char *propType = property_getAttributes(property);
        if(propType&&propName) {
            NSArray *anAttribute = [[NSString stringWithUTF8String:propType]componentsSeparatedByString:@","];
            NSString *aType = [anAttribute firstObject];
            [dict setObject:aType forKey:[NSString stringWithUTF8String:propName]];
        }
    }
    free(properties);
    
    return dict;
}

//静态就交换静态，实例方法就交换实例方法
void Swizzle(Class c, SEL origSEL, SEL newSEL)
{
    //获取实例方法
    Method origMethod = class_getInstanceMethod(c, origSEL);
    Method newMethod = nil;
    if (!origMethod) {
        //获取静态方法
        origMethod = class_getClassMethod(c, origSEL);
        newMethod = class_getClassMethod(c, newSEL);
    }else{
        newMethod = class_getInstanceMethod(c, newSEL);
    }
    
    if (!origMethod||!newMethod) {
        return;
    }
    
    //自身已经有了就添加不成功，直接交换即可
    if(class_addMethod(c, origSEL, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))){
        //添加成功一般情况是因为，origSEL本身是在c的父类里。这里添加成功了一个继承方法。
        class_replaceMethod(c, newSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    }else{
        method_exchangeImplementations(origMethod, newMethod);
    }
}

NSDictionary *FunctionCallerMessage()
{
#ifdef DEBUG
    NSArray *symbols = [NSThread callStackSymbols];
    if (symbols.count<3) {
        return nil;
    }
    NSString *sourceString = symbols[2];
    if (sourceString.length<=0) {
        return nil;
    }
    
    //还是不用正则了。感觉性能不会太好
    //找到开始中括号
    NSRange range = [sourceString rangeOfString:@"["];
    if (range.length<=0||range.location<=0) {
        return nil;
    }
    
    BOOL isInstance = [[sourceString substringWithRange:NSMakeRange(range.location-1,1)] isEqualToString:@"+"];
    
    sourceString = [sourceString substringFromIndex:range.location+1];
    
    //找到结束中括号
    range = [sourceString rangeOfString:@"]"];
    if (range.length<=0) {
        return nil;
    }
    sourceString = [sourceString substringToIndex:range.location];
    
    NSMutableArray *array = [[sourceString componentsSeparatedByString:@" "]mutableCopy];
    if (array.count!=2) {
        return nil;
    }
    
    NSMutableDictionary *message = [NSMutableDictionary dictionary];
    
    //是否是类目 是的话去掉类目名字
    range = [array[0] rangeOfString:@"("];
    if (range.length>0) {
        NSString *categoryName = [array[0] substringWithRange:NSMakeRange(range.location+1, ((NSString*)array[0]).length-(range.location+1)-1)];
        message[@"Category"] = categoryName;
        
        
        array[0] = [array[0] substringToIndex:range.location];
    }
    message[@"Class"] = NSClassFromString(array[0]);
    message[@"Method"] = array[1];
    message[@"IsInstance"] = @(isInstance);
    
    return message;
#else
    return nil;
#endif
}
